const util = require("../../utils/config.js");
//获取应用实例
const app = getApp()
const http_host = util.http_host;
const agent_id = util.agent_id;
const img_url = util.img_url;
const appId = util.appId;
const secret = util.secret;
Page( {
  data: {
    index: 0,
    tishi:'',
    //日期是否改变
    rqgb:'',
    start:'',
    end:'',
    date: '2018-04-01',
    time: '12:00',
    bookToastHidden: true,
    tableClass:'',
    tableInfo:'',
    //联系人
    link: '',
    //联系电话
    tel:'',
    //桌子类型
    tablclassify:'',
    //留言内容
    message:'',
    //当前商家的id
    shopId:'',
  },
  onLoad: function (options) {

    var that=this;


    //当前页面数据处理
    wx.request({
      url: http_host + 'bookjz',
      data: {

      },
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        console.log(1)
        console.log(res)
        that.setData({
          start:res.data.start,
          end:res.data.end,

        })


    // 设置好当前的商家
    that.setData({
      artype:options.artype,
    })
    var shopId=options.id;

    that.setData({
      shopId: shopId
    })
    //获取当前商家的餐桌类型
    wx.request({
      url: http_host + 'getSelected',
      data: {
        id: shopId
      },
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        that.setData({
          tableClass: res.data,
          tablclassify: res.data[0]
        });
        //获取当前商家的信息
        wx.request({
          url: http_host + 'getTableShop',
          data: {
            shopId: shopId
          },
          header: {
            'Content-Type': 'application/json'
          },
          success: function (res) {
            that.setData({
              tableInfo: res.data
            })
          }
        })
      }
    })
      }
    })
  },
  // 餐桌大小选择
  bindAddrPickerChange: function (e) {
    var table = this.data.tableClass;
    //改变index值，通过setData()方法重绘界面
    var data = table[e.detail.value];
    for (var index in table){
      if (table[index]==data){
        this.setData({
          index: index
        })
      }
    }
    this.setData({
      tablclassify: data
    });
  },
  //获取联系人
  setLink:function(e){
    this.setData({
      tel: e.detail.value
    })
  },
  //获取电话号码
  setTel:function(e){
    this.setData({
      link: e.detail.value
    })
  },
  //获取到留言信息
  setMessage:function(e){
    this.setData({
      message: e.detail.value
    })
  },
  yuding:function(res){
console.log(app)
    var that = this;
    that.setData({
          bookToastHidden:false
      })
      //发送预定的消息

      var openId = app.globalData.openId;
      var person_name = that.data.tel;    //联系人的名字
      var person_tel = that.data.link;    //联系人的电话
      var person_remark = that.data.message;    //联系人的留言信息
      var date = that.data.date;    //预定的日期
      var time = that.data.time;    //预定的时间
      var shopId = that.data.shopId;    //当前商家
      var tablclassify = that.data.tablclassify;    //当前的大桌

      if (person_name == '' || person_tel == '' || !that.data.riqi == 1)
      {

        if (person_name == '') {
          that.setData({
            tishi:'请填写联系人名称'
          })

        }else if(person_tel == ''){
          that.setData({
            tishi:'请填写联系人电话'
          })
        } else if (!that.data.riqi == 1)
        {
          that.setData({
            tishi: '请填写有效时间'
          })
        }
        wx.showModal({
          title: '温馨提示',
          content: that.data.tishi,
          success: function (res) {


          }
        })
      }else{
      wx.request({
        url: http_host + 'quhao',
        data: {
          form_id:res.detail.formId,
          openid: openId,
          name : person_name,
          tel: person_tel,
          mark: person_remark,
          ymd:date,
          time: time,
          shopid: shopId,
          quyu: tablclassify,
          agent_id:agent_id,
        },
        header: {
          'Content-Type': 'application/json'
        },
        success: function (res) {
          console.log(3)
          console.log(res);

          if(res.data == '餐桌空')
          {
            wx.showModal({
              title: '温馨提示',
              content: '当前区域没有空余餐桌，换个区域试试？',
              success: function (res) {


              }
            })
          }else if(res.data){
            wx.showToast({
              title: '请等待后台处理',
              icon: 'success',
              duration: 2000,
              mask: true
            });
          }
        }
      })


      }
      //   // wx.reLaunch({
      //   //   url: '../index/index'
      //   // })
  },
  hideToast:function(){
    this.setData({
          bookToastHidden:true
      })
  },
  // 日期选择
  bindDateChange: function(e){
    console.log('date picker发送选择改变，携带值为', e.detail.value)
    this.setData({
          date: e.detail.value,
          riqi:1,
    })
  },
  // 时间选择
  bindTimeChange: function(e){
    console.log('time picker发送选择改变，携带值为', e.detail.value)
    this.setData({
          time: e.detail.value
    })
  },
  calling: function () {
    var that = this;
    var phone = that.data.tableInfo.business_tel;
    wx.makePhoneCall({
      phoneNumber: phone,
      success: function () {
        console.log("拨打电话成功！")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '微信小程序',
      desc: '预定',
      path: '/pages/user?id=123'
    }
  },
});